package com.example.ch05.intent;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.example.ch05.R;

public class DataTransferFirstActivity extends AppCompatActivity {

    private static final String TAG = "DataTransferFirstActivity";
    private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                final int resultCode = result.getResultCode();
                final Intent data = result.getData();
                Log.d(TAG, " resultCode: " + resultCode + " data: " + data);

                if (result.getResultCode() == AppCompatActivity.RESULT_OK) {
                    if (data.hasExtra("returnKey")) {
                        Toast.makeText(
                                this,
                                "通过第一个按钮返回的结果："
                                        + data.getExtras().getString("returnKey"),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });

    private final ActivityResultLauncher<Intent> mStartForResult2 = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                final int resultCode = result.getResultCode();
                final Intent data = result.getData();
                Log.d(TAG, " resultCode: " + resultCode + " data: " + data);
                if (result.getResultCode() == AppCompatActivity.RESULT_OK) {
                    // 処理成功時
                    if (data.hasExtra("returnKey")) {
                        Toast.makeText(
                                this,
                                "通过第二个按钮返回的结果："
                                        + data.getExtras().getString("returnKey"),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
    private Intent i;
    private EditText edittext01;
    private EditText edittext02;
    private Button button01;
    private Button button02;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c05_activity_first_layout);
        i = new Intent();
        i.setComponent(new ComponentName(
                "com.example.ch05",
                "com.example.ch05.intent.DataTransferSecondActivity"));
        edittext01 = (EditText) findViewById(R.id.edittext01);
        edittext02 = (EditText) findViewById(R.id.edittext02);
        button01 = (Button) findViewById(R.id.button01);
        button01.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                i.putExtra("Value", edittext01.getText().toString());
                mStartForResult.launch(i);
            }
        });
        button02 = (Button) findViewById(R.id.button02);
        button02.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                i.putExtra("Value", edittext02.getText().toString());
                mStartForResult2.launch(i);
            }
        });
    }
}
